package com.gbs.flash.api.controller.promotion;

import com.gbs.flash.bean.constant.factory.PageFactory;
import com.gbs.flash.bean.core.BussinessLog;
import com.gbs.flash.bean.entity.promotion.Topic;
import com.gbs.flash.bean.enumeration.Permission;
import com.gbs.flash.bean.exception.ApplicationException;
import com.gbs.flash.bean.exception.ApplicationExceptionEnum;
import com.gbs.flash.bean.vo.front.Rets;
import com.gbs.flash.bean.vo.query.SearchFilter;
import com.gbs.flash.service.promotion.TopicService;
import com.gbs.flash.utils.DateUtil;
import com.gbs.flash.utils.StringUtil;
import com.gbs.flash.utils.factory.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "专题管理", tags = "专题管理")
@RestController
@RequestMapping("/promotion/topic")
public class TopicController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private TopicService topicService;

    @ApiOperation("查询所有专题文章")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list(@RequestParam(value = "disabled", required = false) Boolean disabled,
                       @RequestParam(value = "startDate", required = false) String startDate,
                       @RequestParam(value = "endDate", required = false) String endDate) {
        Page<Topic> page = new PageFactory<Topic>().defaultPage();
        page.addFilter("disabled", disabled);
        page.addFilter("createTime", SearchFilter.Operator.GTE, DateUtil.parse(startDate, "yyyyMMddHHmmss"));
        page.addFilter("createTime", SearchFilter.Operator.LTE, DateUtil.parse(endDate, "yyyyMMddHHmmss"));
        page = topicService.queryPage(page);
        return Rets.success(page);
    }

    @ApiOperation("编辑专题")
    @RequestMapping(method = RequestMethod.POST)
    @BussinessLog(value = "编辑专题", key = "name")
    @RequiresPermissions(value = {Permission.TOPIC_EDIT})
    public Object save(@ModelAttribute Topic topic) {
        if (topic.getId() == null) {
            topic.setPv(0L);
            topicService.insert(topic);
        } else {
            topicService.update(topic);
        }
        return Rets.success();
    }

    @ApiOperation("删除专题")
    @RequestMapping(method = RequestMethod.DELETE)
    @BussinessLog(value = "删除专题", key = "id")
    @RequiresPermissions(value = {Permission.TOPIC_DEL})
    public Object remove(Long id) {
        if (StringUtil.isEmpty(id)) {
            throw new ApplicationException(ApplicationExceptionEnum.REQUEST_NULL);
        }
        topicService.deleteById(id);
        return Rets.success();
    }

    @ApiOperation("禁用/启用专题")
    @RequestMapping(value = "/changeDisabled", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permission.TOPIC_EDIT})
    public Object changeIsOnSale(@RequestParam("id") Long id, @RequestParam("disabled") Boolean disabled) {
        if (id == null) {
            throw new ApplicationException(ApplicationExceptionEnum.REQUEST_NULL);
        }
        topicService.changeDisabled(id, disabled);
        return Rets.success();
    }
}
